package com.zhuanzhuan.hero.user.facade;

import cn.hutool.core.util.ObjectUtil;
import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.google.common.collect.Lists;
import com.zhuanzhuan.hero.user.dto.SetupUserPrivacyDTO;
import com.zhuanzhuan.hero.user.dto.privacy.QueryPrivacyUserDTO;
import com.zhuanzhuan.hero.user.dto.privacy.SwitchPrivacyDTO;
import com.zhuanzhuan.hero.user.entity.HeroUserPrivacySetup;
import com.zhuanzhuan.hero.user.enums.PrivacyTypeEnum;
import com.zhuanzhuan.hero.user.service.HeroUserInfoService;
import com.zhuanzhuan.hero.user.service.HeroUserPrivacySetupService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.PageResultWrapper;
import com.zhuanzhuan.hero.user.vo.UserListItemVO;
import com.zhuanzhuan.hero.user.vo.privacy.ShowVisitRecordSwitchVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserPrivacyFacadeImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/1 19:22
 */
@ServiceBehavior
@Slf4j
public class UserPrivacyFacadeImpl implements IUserPrivacyFacade {

    @Resource
    private HeroUserPrivacySetupService userPrivacySetupService;

    @Resource
    private HeroUserInfoService userInfoService;

    @ZZMethod(value = "queryShowVisitRecordSwitch", desc = "查询隐私设置开关", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<ShowVisitRecordSwitchVO> queryShowVisitRecordSwitch(@ZZHeader ServiceHeaderEntity header) {
        return ZZOpenScfBaseResult.buildSucc(userPrivacySetupService.queryShowVisitRecordSwitch(ServiceHeaderUtil.parseUid(header)));
    }

    @ZZMethod(value = "switchPrivacy", desc = "隐私设置开关", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> switchPrivacy(@ZZHeader ServiceHeaderEntity header,
                                                      @ZZParam("param") @Valid SwitchPrivacyDTO switchPrivacyDTO) {
        Long uid = ServiceHeaderUtil.parseUid(header);
        userInfoService.checkCanEdit(uid);
        userPrivacySetupService.switchPrivacy(uid, switchPrivacyDTO);
        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @ZZMethod(value = "pagePrivacyUser", desc = "分页查询隐私设置用户列表", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<PageResultWrapper<UserListItemVO>> pagePrivacyUser(@ZZHeader ServiceHeaderEntity header,
                                                                                  @ZZParam("param") @Valid QueryPrivacyUserDTO queryPrivacyUserDTO) {
        return ZZOpenScfBaseResult.buildSucc(userPrivacySetupService.pagePrivacyUser(ServiceHeaderUtil.parseUid(header),
                queryPrivacyUserDTO.getPrivacyType(), queryPrivacyUserDTO));
    }

    @ZZMethod(value = "setupPrivacy", desc = "对某一用户设置不看ta、不让ta看", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> setupPrivacy(@ZZHeader ServiceHeaderEntity header,
                                                     @ZZParam("param") @Valid SetupUserPrivacyDTO setupUserPrivacyDTO) {
        Integer isInvisible = null, isBlock = null;
        Integer privacyType = setupUserPrivacyDTO.getPrivacyType();
        if (PrivacyTypeEnum.INVISIBLE.getCode().equals(privacyType)) {
            isInvisible = setupUserPrivacyDTO.getSwitchState();
        }
        if (PrivacyTypeEnum.BLOCK.getCode().equals(privacyType)) {
            isBlock = setupUserPrivacyDTO.getSwitchState();
        }

        Long uid = ServiceHeaderUtil.parseUid(header);
        userInfoService.checkCanEdit(uid);
        userPrivacySetupService.setupPrivacy(uid, setupUserPrivacyDTO.getToUid(), isInvisible,
                isBlock);
        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @Override
    public Result<List<Long>> queryPrivacyUids(@NotNull Long uid, @Valid QueryPrivacyUserDTO queryPrivacyUserDTO) {
        List<HeroUserPrivacySetup> setups =
                userPrivacySetupService.listByUidAndPrivacyType(uid, queryPrivacyUserDTO.getPrivacyType());
        return Result.success(setups.stream().map(HeroUserPrivacySetup::getToUid).distinct().collect(Collectors.toList()));
    }

    /**
     * 隐私过滤: 我不看他, 不让他看
     *
     * @param uid
     * @return
     */
    @Override
    public Result<List<Long>> queryIgnoreTypePrivacyUids(Long uid) {

        //2-不让ta看
        //SELECT * FROM `hero_user_privacy_setup` where state=1 and privacy_type=2 and to_uid=1726933674274684032 union
        //1-不看ta
        //SELECT * FROM `hero_user_privacy_setup` where state=1 and privacy_type=1 and from_uid=1726933674274684032;

        List<Long> result = Lists.newArrayList();
        //1-不看ta
        List<HeroUserPrivacySetup> fromList = userPrivacySetupService.listByUidAndPrivacyType(uid, 1);
        if (ObjectUtil.isNotEmpty(fromList)) {
            result.addAll(fromList.stream().map(HeroUserPrivacySetup::getToUid).distinct().collect(Collectors.toList()));
        }
        //2-不让ta看
        List<HeroUserPrivacySetup> toList = userPrivacySetupService.listByToUidAndPrivacyType(uid, 2);
        if (ObjectUtil.isNotEmpty(toList)) {
            result.addAll(toList.stream().map(HeroUserPrivacySetup::getFromUid).distinct().collect(Collectors.toList()));
        }

        return Result.success(result);
    }
}
